<?php
//$stem = "십";
//$tense = "present";
//$politeness = "standard";

$stem = $_POST["stem"];
$tense = $_POST["tense"];
$politeness = $_POST["politeness"];

# present tense
$present["formal_h"] 		= "십니다";
$present["formal"] 			= "(습,ㅂ)니다";
$present["standard_h"]		= "(으세,세)요";
$present["standard"]		= "(어,아,여)요";
$present["casual"]			= "(어,아,여)";
$present["low"]				= "(는,ㄴ)다";

# past tense
$past["formal_h"] 			= "(으셨,셨)습니다";
$past["formal"] 			= "(었,았,였)습니다";
$past["standard_h"]			= "(으셨,셨)어요";
$past["standard"]			= "(었,았,였)어요";
$past["casual"]				= "(었,았,였)어";
$past["low"]				= "(었,았,였)다";

# definite future tense
$d_future["formal_h"] 		= "(으시,시)겠습니다";
$d_future["formal"]			= "겠습니다";
$d_future["standard_h"]		= "(으시,시)겠어요";
$d_future["standard"]		= "겠어요";
$d_future["casual"]			= "겠어";
$d_future["low"]			= "겠다";

# probable future tense
$p_future["formal_h"]		= "(으실,실)겁니다";
$p_future["formal"]			= "(을,ᄅ)겁니다"; 
$p_future["standard_h"]		= "(으)실거예요";
$p_future["standard"]		= "(으)ㄹ거예요";
$p_future["casual"]			= "(으)ㄹ거야";
$p_future["low"]			= "(으)ㄹ거다";

# imperative tense
$imperative["formal_h"]		= "(으십,십)시오";
$imperative["formal"]		= "(으십,십)시오";
$imperative["standard_h"] 	= "(으)세요";
$imperative["standard"]		= "(어,아,여)요"; 
$imperative["casual"]		= "(어,아,여)";
$imperative["low"] 			= "(어,아,여)라";

# proposition tense
$proposition["formal_h"]	= "(읍,ᄇ)시다";
$proposition["formal"]		= "(읍,ㅂ)시다";
$proposition["standard_h"]	= "(어,아,여)요"; 
$proposition["standard"]	= "(어,아,여)요";
$proposition["casual"]		= "(어,아,여)";
$proposition["low"]			= "자";
	
# grammar rules
$rule["present"] = $present;
$rule["past"] = $past;
$rule["d_future"] = $d_future;
$rule["p_future"] = $p_future;
$rule["imperative"] = $imperative;
$rule["proposition"] = $proposition;

$suffix = $rule[$tense][$politeness];
$conjugation = Conjugate($stem, $suffix);

$msg = "<p>Stem: <span class=\"bold\">" . $stem . "</span><br>";
$msg .= "Suffix: <span class=\"bold\">" . $suffix . "</span><br>";
$msg .= "Conjugation: <span class=\"bold\">TBD</span>";
$msg .="</p>";
echo $msg;

/**
 * utf8_to_html
 * Converts a utf8 string to its html code equivalent.
 * @param string
 * @return string
 */
function utf8_to_html ($data) {
    return preg_replace("/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e", '_utf8_to_html("\\1")', $data);
}

function _utf8_to_html ($data) {
    $ret = 0;
    foreach((str_split(strrev(chr((ord($data{0}) % 252 % 248 % 240 % 224 % 192) + 128) . substr($data, 1)))) as $k => $v) {
    	$ret += (ord($v) % 128) * pow(64, $k);
    }
    return $ret;
}

/** 
 * Conjugate
 * Conjugate the given verb stem with 
 * the given suffix.
 * 
 * @param 	string
 * @param 	string
 * @return 	string
 */
function Conjugate($v, $s) {
	return $v . $s;	
}

/** 
 * Conjugate
 * Conjugate the given verb stem with 
 * the given suffix where the verb stem
 * ends in '하'.
 * 
 * @param 	string
 * @param 	string
 * @return 	string
 */
function Conjugate1($v, $s) {
	return $v . $s;
}

/** 
 * Conjugate
 * Conjugate the given verb stem with 
 * the given suffix where the verb stem
 * ends in a vowel.
 * 
 * @param 	string
 * @param 	string
 * @return 	string
 */
function Conjugate2($v, $s) {
	return $v . $s;
}

/** 
 * Conjugate
 * Conjugate the given verb stem with 
 * the given suffix where the verb stem
 * ends in a consonant.
 * 
 * @param 	string
 * @param 	string
 * @return 	string
 */
function Conjugate3($v, $s) {
//	$sparen = strpos($s,"(");
//	$append = $s;
//	if ( $sparen !== FALSE ) {
//		$eparen = strpos($s,")");
//		if ( $eparen !== FALSE ) {
//			$inside = substr($s,$sparen+1,$eparen-$sparen-1);
//			$remainder = substr($s,$eparen+1);
//			$prefix = explode(",",$inside);
//			if ( count($prefix) == 2 ) {
//				$append = $prefix[0] . $remainder;
//			} else {
//			}			
//		}
//	}
	return $v . $s;
}
?>
